package sgii;

import java.io.File;
import java.io.IOException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Nicolas
 */
public class XmlReader {
            
    public static void GetClients(String fileLocation, String tableName, String type) throws Exception{
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
             DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
             File customerW = new File(fileLocation);
             Document xmldoc = docBuilder.parse(customerW);
             xmldoc.getDocumentElement().normalize();
             System.out.println ("El elemento raíz es " +
             xmldoc.getDocumentElement().getNodeName());
             int i;
             
             
             String state,zipcode,city,birth,id,name;
             NodeList CustomerList = xmldoc.getElementsByTagName("cliente");
             for(i=0;i < CustomerList.getLength(); i++){
                 String sql = "INSERT INTO Customers VALUES (";
                 state=zipcode=city=birth=id=name="Vacio";
                 Node personNode = CustomerList.item(i);
                 Element personElement = (Element)personNode;                 
                 
                 //Levanto los campos
                 id = XmlReader.GetNodeField("CUSTOMER_ID",personElement);
                 sql += Formatter.getFormattedString(id, 2) + ",";
                 name = XmlReader.GetNodeField("FULL_NAME",personElement);
                 sql += Formatter.getFormattedString(name, 1) + ",";
                 birth = XmlReader.GetNodeField("BIRTH_DATE",personElement);
                 sql += Formatter.getFormattedString(birth, 3) + ",";
                 city = XmlReader.GetNodeField("CITY",personElement);
                 sql += Formatter.getFormattedString(city, 1) + ",";
                 state = XmlReader.GetNodeField("STATE",personElement);
                 sql += Formatter.getFormattedString(state, 1) + ",";
                 zipcode = XmlReader.GetNodeField("ZIPCODE",personElement);
                 sql += Formatter.getFormattedString(zipcode, 1);
                 //System.out.println("Customer" + i + ":" + name + " "+ id + " "+ birth + " "+ city + " "+ state + " "+ zipcode );
                 sql += ",'" + type + "')";
                Statement st = SGII.connection.createStatement();
                System.out.println(sql);
                st.executeUpdate(sql);                             
             }
    }
    
    public static String GetNodeField( String nodeField, Element nodeElement){
         NodeList fieldList = nodeElement.getElementsByTagName(nodeField);
         Element fieldElement = (Element)fieldList.item(0);
         String fieldValue = fieldElement.getChildNodes().item(0).getNodeValue();
         return fieldValue;
    }
}
